// JavaScript Document
var showmeta  = 0;
var content = {

    changeTab : function(tabName) {
		// validate
		var left_content = document.getElementById('div_left_content');
		var left_tab = document.getElementById('left');
        var middle_content = document.getElementById('div_middle_content');
		var middle_tab = document.getElementById('middle');
		var right_content = document.getElementById('div_right_content');
		var right_tab = document.getElementById('right');
		if (tabName == 'left')
		{
			left_content.style.display = 'block';
			middle_content.style.display = 'none';
			right_content.style.display = 'none';
			
			left_tab.className = 'current';
			middle_tab.className = 'none';
			right_tab.className = 'none';
		}
		else if (tabName == 'right'){
			left_content.style.display = 'none';
			middle_content.style.display = 'none';
			right_content.style.display = 'block';
			
			left_tab.className = 'none';
			middle_tab.className = 'none';
			right_tab.className = 'current';
		}
		else {
			left_content.style.display = 'none';
			middle_content.style.display = 'block';
			right_content.style.display = 'none';
			
			left_tab.className = 'none';
			middle_tab.className = 'current';
			right_tab.className = 'none';
		}
		
		return true;
    },

    changeImage: function (){
		var hd_page_active = document.getElementById('hd_page_active');
		var img_page_active = document.getElementById('img_page_active');
		if(hd_page_active.value==1){
			img_page_active.src = BASE_URL+"application/assets/images/deactive.gif";
			//alert(STR_URL)
			hd_page_active.value = 0;
		}
		else{
			img_page_active.src = BASE_URL+"application/assets/images/active.gif";
			hd_page_active.value = 1;
		}
		
	},

    showMeta: function (object){
		$('block_meta').toggle();
        if($('block_meta').style.display == 'none')
            object.innerHTML = "&nbsp;Add Meta Tags (+)";
        else
            object.innerHTML = "&nbsp;Add Meta Tags (-)";
	},

    delItem: function (){
		var page_id = document.getElementById('page_id').value;
		var answer = confirm("Are you sure delete this page?");
		if (answer){
			var url = SITE_URL+"admin_ajax/content_management.content_process.del_page/"+page_id;
            new Ajax.Request(url, {   method: 'post',   
                onSuccess: function(transport) {
                    if(transport.responseText.match("<success>")){
                        document.location.reload();
                    }else
                        alert(transport.responseText);
                }
            });
		}
	},

    cancelClick: function (){
		var page_content = document.getElementById('page_content');
		var answer = confirm("Are you sure close content page?");
		if (answer){			
			page_content.innerHTML = "";
			//page_content.style.display ="none";
		}					
		
	},

    saveItem: function (){
		var url = SITE_URL+"admin_ajax/content_management.content_management.save_content";		
		var page_active = document.getElementById('hd_page_active');
		var page_id = document.getElementById('page_id');		
		var title = document.getElementById('_title');
		var description = document.getElementById('_description');
		var keywords = document.getElementById('_keywords');
		var page_visible = (document.getElementById('page_visible').checked==true)?1:0;
		//var middle_content = document.getElementById('middle_content');
		var oEditor = FCKeditorAPI.GetInstance('middle_content') ;
		var middle_content = oEditor.GetXHTML(true);
		var left_content = FCKeditorAPI.GetInstance('left_content').GetXHTML(true);
		var right_content = FCKeditorAPI.GetInstance('right_content').GetXHTML(true);
		var item_visible = (document.getElementById('menu_visible').checked==true)?1:0;
		
		new Ajax.Request(url, {   method: 'post',   
			parameters: {
			  page_id: page_id.value,
			  page_visible: page_visible,
			  page_active: page_active.value,
			  page_content_middle: middle_content,
			  page_meta_title: title.value,
			  page_meta_description: description.value,
			  page_meta_keyword: keywords.value,
			  page_content_left: left_content,
			  page_content_right: right_content,
			  item_visible: item_visible
		   },
			onSuccess: function(transport) {     
				if(transport.responseText.match("<success>")){
					var re_data = transport.responseText
					alert('Page saved.')
				}
			},
			onFailure: function(){} 
		});
		
	},
    
    dropItem: function(dragId, dropId){
		var instance = "";
		if(dropId=="div_right_content") instance = "right_content";
		else if(dropId=="div_left_content") instance = "left_content";
		else instance = "middle_content";
		var oEditor = FCKeditorAPI.GetInstance(instance) ;
		var old_content = oEditor.GetXHTML(true);
		var new_content = old_content + "<p>{" + dragId + "}</p>";
		oEditor.SetHTML(new_content);
	},
    
    pageClick :function (page_id, nav) {
        var url = SITE_URL + "admin_ajax/content_management.content_management.get_content";
        new Ajax.Request(url, {   method: 'post',   
            parameters: {
              page_id: page_id,
              nav: nav
           },
            onSuccess: function(transport) {     
                if(transport.responseText){
                    //cancelDroppables();
                    var re_data = transport.responseText
                    var page_content = document.getElementById('page_content');
                    page_content.innerHTML = re_data;
                    page_content.style.display = "block";
                    //setDroppables();
                }
            },
            onFailure: function(){} 
        });
    },
    
    pageDelete : function (branch){
        current_page = null;
        current_item = null; 
        var arr_item = branch.getId().split("_");
        var page_id = arr_item[1];
        var answer = confirm("Are you sure delete this page?");
        if (answer){
            var url = SITE_URL+"admin_ajax/content_management.content_process.del_page/"+page_id;
            new Ajax.Request(url, {   method: 'post',   
                onSuccess: function(transport) {
                    if(transport.responseText.match("<success>")){
                        parent.content.pageClose(page_id);
                        document.location.reload();
                    }else
                        alert(transport.responseText);
                }
            });
        }
    },
    
    pageClose : function(page_id){
        try{
            var opening_page_id = $F('page_id');
            // if deleted page is opening, then close it
            if(opening_page_id == page_id){
                var page_content = $('page_content');
                page_content.innerHTML = '';
                page_content.style.display = "none";
            }
        }catch(ex){}
    },
    
    pageAdd : function(par_id){
        var url = SITE_URL+"admin_ajax/content_management.content_process.add_page/"+par_id;
        new Ajax.Request(url, {   method: 'post',   
            onSuccess: function(transport) {
                if(transport.responseText.match("<success>")){
                    document.location.reload();
                }else
                    alert(transport.responseText);
            }
        });
    },
            
    navigationDelete : function (branch){
        current_page = null;
        current_item = null; 
        var arr_item = branch.getId().split("_");
        var nav_id = arr_item[0];
        var answer = confirm("Are you sure delete this menu item?");
        if (answer){
            var url = SITE_URL+"admin_ajax/content_management.content_process.del_navigation/"+nav_id;
            new Ajax.Request(url, {   method: 'post',   
                onSuccess: function(transport) {
                    if(transport.responseText.match("<success>")){
                        document.location.reload();
                    }else
                        alert(transport.responseText);
                }
            });
        }
    },
    
    navigationAdd : function (par_id){
        var url = SITE_URL + "admin_ajax/content_management.content_process.add_submenu";
        new Ajax.Request(url, {   method: 'post',   
                parameters: {
                  par_id: par_id
               },
                onSuccess: function(transport) {     
                    if(transport.responseText.match("<success>")){
                        document.location.reload();
                    }else
                        alert(transport.responseText);
                },
                onFailure: function(){} 
        });
    },
    
    navigationMoveUp : function(nav_id){
        content.navigationMove(nav_id, 'move_up');
    },
    
    navigationMoveDown : function(nav_id){
        content.navigationMove(nav_id, 'move_down');
    }, 
    
    navigationMove : function(nav_id, direction){
        var url = SITE_URL + "admin_ajax/content_management.content_process." + direction;
        new Ajax.Request(url, {   method: 'post',   
                parameters: {
                  nav_id: nav_id
               },
                onSuccess: function(transport) {     
                    if(transport.responseText.match("<success>")){
                        document.location.reload();
                    }else if(transport.responseText.match("<nochange>")){
                        return false;
                    }else
                        alert(transport.responseText);
                },
                onFailure: function(){} 
        });
    }
}
